package com.imedway.ihe.dtz.xds.dao.camel;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
import org.springframework.stereotype.Repository;
import org.springframework.util.StringUtils;

import com.imedway.ihe.dd.vo.FindDocumentsVO;
import com.imedway.ihe.dd.vo.FindFoldersVO;
import com.imedway.ihe.dd.vo.FindSubmissionSetsVO;
import com.imedway.ihe.dtz.xds.entity.camel.XdsAssociationPO;
import com.imedway.ihe.dtz.xds.entity.camel.XdsCache;
import com.imedway.ihe.dtz.xds.entity.camel.XdsDocumentEntryPO;
import com.imedway.ihe.dtz.xds.entity.camel.XdsFolderPO;
import com.imedway.ihe.dtz.xds.entity.camel.XdsSubmissionSetPO;
import com.imedway.ihe.dtz.xds.vo.GetDocumentsVO;

import cn.hutool.json.JSONUtil;
import lombok.extern.slf4j.Slf4j;

/**
 * @author dtz <br/>
 * @date 2024/7/4
 */
@Slf4j
@Repository
public class FindBaseDao_CAMEL {
	
	@Resource
	private JdbcTemplate jdbcTemplate;
		
	@Resource
	private NamedParameterJdbcTemplate namedParameterJdbcTemplate;
	
	
	public List<String> findExtrinsicobject(FindDocumentsVO vo )  {
		log.info("入参:"+JSONUtil.toJsonStr(vo));
		StringBuilder sql = new StringBuilder();
		Map<String, String> map = new HashMap<String, String>();
		sql.append(" SELECT entryUuid FROM camel_xdsdocumententry where 1=1 ");
		if (StringUtils.hasText(vo.get$XDSDocumentEntryAuthorPerson())) {
		
		}
		if (StringUtils.hasText(vo.get$XDSDocumentEntryClassCode())) {
			sql.append("  and classCode=:classCode ");
			map.put("classCode", vo.get$XDSDocumentEntryClassCode());
		}
		if (StringUtils.hasText(vo.get$XDSDocumentEntryCreationTimeFrom())) {
			sql.append("  and creationTime>=:creationTime ");
			map.put("creationTime", vo.get$XDSDocumentEntryCreationTimeFrom());
		}
		if (StringUtils.hasText(vo.get$XDSDocumentEntryCreationTimeTo())) {
			sql.append("  and creationTime<=:creationTime ");
			map.put("creationTime", vo.get$XDSDocumentEntryCreationTimeTo());
		}
		if (StringUtils.hasText(vo.get$XDSDocumentEntryServiceStartTimeFrom())) {
			sql.append("  and serviceStartTime>=:serviceStartTime ");
			map.put("serviceStartTime", vo.get$XDSDocumentEntryServiceStartTimeFrom());
		}
		if (StringUtils.hasText(vo.get$XDSDocumentEntryServiceStartTimeTo())) {
			sql.append("  and serviceStartTime<=:serviceStartTime ");
			map.put("serviceStartTime", vo.get$XDSDocumentEntryServiceStartTimeTo());
		}
		if (StringUtils.hasText(vo.get$XDSDocumentEntryHealthcareFacilityTypeCode())) {
			sql.append("  and healthcareFacilityTypeCode=:healthcareFacilityTypeCode ");
			map.put("healthcareFacilityTypeCode", vo.get$XDSDocumentEntryHealthcareFacilityTypeCode());
		}
		if (StringUtils.hasText(vo.get$XDSDocumentEntryPatientId())) {
			sql.append("  and patientId=:patientId ");
			map.put("patientId", vo.get$XDSDocumentEntryPatientId());
		}
		if (StringUtils.hasText(vo.get$XDSDocumentEntryStatus())) {
			sql.append("  and availabilityStatus=:availabilityStatus ");
			map.put("availabilityStatus", vo.get$XDSDocumentEntryStatus());
		}
		if (StringUtils.hasText(vo.get$XDSDocumentEntryTypeCode())) {
			sql.append("  and documentEntryType=:documentEntryType ");
			map.put("documentEntryType", vo.get$XDSDocumentEntryTypeCode());
		}
		log.info(sql.toString());
		List<String> stringList = namedParameterJdbcTemplate.queryForList(sql.toString(), map, String.class);
		log.info(JSONUtil.toJsonStr(stringList));
		return stringList;
	}
	
	public List<String> FIND_SUBMISSION_SETS(FindSubmissionSetsVO vo )  {
		log.info("入参:"+JSONUtil.toJsonStr(vo));
		StringBuilder sql = new StringBuilder();
		Map<String, String> map = new HashMap<String, String>();
		sql.append(" SELECT entryUuid  FROM camel_xdssubmissionset where 1=1 ");
		if (StringUtils.hasText(vo.get$XDSSubmissionSetPatientId())) {
			sql.append("  and patientId=:patientId ");
			map.put("patientId", vo.get$XDSSubmissionSetPatientId());
		}
		if (StringUtils.hasText(vo.get$XDSSubmissionSetSourceId())) {
			sql.append("  and sourceId=:sourceId ");
			map.put("sourceId", vo.get$XDSSubmissionSetSourceId());
		}
		if (StringUtils.hasText(vo.get$XDSSubmissionSetContentType())) {
			sql.append("  and contentTypeCode=:contentTypeCode ");
			map.put("contentTypeCode", vo.get$XDSSubmissionSetContentType());
		}
		if (StringUtils.hasText(vo.get$XDSSubmissionSetStatus())) {
			sql.append("  and availabilityStatus=:availabilityStatus ");
			map.put("availabilityStatus", vo.get$XDSSubmissionSetPatientId());
		}
		if (StringUtils.hasText(vo.get$XDSSubmissionSetSubmissionTimeFrom())) {
			sql.append("  and submissionTime>=:submissionTime ");
			map.put("submissionTime", vo.get$XDSSubmissionSetSubmissionTimeFrom());
		}
		if (StringUtils.hasText(vo.get$XDSSubmissionSetSubmissionTimeTo())) {
			sql.append("  and submissionTime<=:submissionTime ");
			map.put("submissionTime", vo.get$XDSSubmissionSetSubmissionTimeTo());
		}
		if (StringUtils.hasText(vo.get$XDSSubmissionSetAuthorPerson())) {

		}
		log.info(sql.toString());
		List<String> stringList = namedParameterJdbcTemplate.queryForList(sql.toString(), map, String.class);
		log.info(JSONUtil.toJsonStr(stringList));
		return stringList;
	}
	
	public List<String> FIND_FOLDERS(FindFoldersVO vo)  {

		StringBuilder sql = new StringBuilder();
		Map<String, String> map = new HashMap<String, String>();
		sql.append(" SELECT entryUuid FROM camel_xdsfolder where 1=1 ");
		if (StringUtils.hasText(vo.get$XDSFolderPatientId())) {
			sql.append("  and patientId=:patientId ");
			map.put("patientId", vo.get$XDSFolderPatientId());
		}
		if (StringUtils.hasText(vo.get$XDSFolderStatus())) {
			sql.append("  and availabilityStatus=:availabilityStatus ");
			map.put("availabilityStatus", vo.get$XDSFolderStatus());
		}
		if (StringUtils.hasText(vo.get$XDSFolderCodeList())) {
			sql.append("  and code=:code ");
			map.put("code", vo.get$XDSFolderCodeList());
		}
		if (StringUtils.hasText(vo.get$XDSFolderLastUpdateTimeFrom())) {
			sql.append("  and lastUpdateTime>=:lastUpdateTime ");
			map.put("lastUpdateTime", vo.get$XDSFolderLastUpdateTimeFrom());
		}
		if (StringUtils.hasText(vo.get$XDSFolderLastUpdateTimeTo())) {
			sql.append("  and lastUpdateTime<=:lastUpdateTime ");
			map.put("lastUpdateTime", vo.get$XDSFolderLastUpdateTimeTo());
		}
		log.info(sql.toString());
		List<String> stringList = namedParameterJdbcTemplate.queryForList(sql.toString(), map, String.class);
		log.info(JSONUtil.toJsonStr(stringList));
		return stringList;
	}
	
	public List<XdsAssociationPO> GET_ASSOCIATIONS(XdsAssociationPO vo )  {
		log.info("入参:"+JSONUtil.toJsonStr(vo));
		StringBuilder sql = new StringBuilder();
		Map<String, String> map = new HashMap<String, String>();
		sql.append(" SELECT * FROM camel_xdsassociation where 1=1 ");
		if (StringUtils.hasText(vo.getEntryUuid())) {
			sql.append("  and entryUuid=:entryUuid ");
			map.put("entryUuid", vo.getEntryUuid());
		}
		if (StringUtils.hasText(vo.getHomeCommunityId())) {
			sql.append("  and homeCommunityId=:homeCommunityId ");
			map.put("homeCommunityId", vo.getHomeCommunityId());
		}
		if (StringUtils.hasText(vo.getSourceUuid())) {
			sql.append("  and sourceUuid=:sourceUuid ");
			map.put("sourceUuid", vo.getSourceUuid());
		}
		if (StringUtils.hasText(vo.getTargetUuid())) {
			sql.append("  and targetUuid=:targetUuid ");
			map.put("targetUuid", vo.getTargetUuid());
		}
		log.info(sql.toString());
//		List<XdsAssociationPO> stringList = namedParameterJdbcTemplate.queryForList(sql.toString(), map, XdsAssociationPO.class);
		List<XdsAssociationPO> stringList = namedParameterJdbcTemplate.query(sql.toString(), map, BeanPropertyRowMapper.newInstance(XdsAssociationPO.class));

		log.info(JSONUtil.toJsonStr(stringList));
		return stringList;
	}
		
	
	public List<XdsDocumentEntryPO> GET_XdsDocumentEntryPO(XdsDocumentEntryPO vo )  {
		log.info("入参:"+JSONUtil.toJsonStr(vo));
		StringBuilder sql = new StringBuilder();
		Map<String, String> map = new HashMap<String, String>();
		sql.append(" SELECT * FROM camel_xdsdocumententry where 1=1 ");
		if (StringUtils.hasText(vo.getHomeCommunityId())) {
			sql.append("  and homeCommunityId=:homeCommunityId ");
			map.put("homeCommunityId", vo.getHomeCommunityId());
		}
		if (StringUtils.hasText(vo.getUniqueId())) {
			sql.append("  and uniqueId=:uniqueId ");
			map.put("uniqueId", vo.getUniqueId());
		}
		if (StringUtils.hasText(vo.getEntryUuid())) {
			sql.append("  and entryUuid=:entryUuid ");
			map.put("entryUuid", vo.getEntryUuid());
		}
		log.info(sql.toString());
//		List<XdsDocumentEntryPO> stringList = namedParameterJdbcTemplate.queryForList(sql.toString(), map, XdsDocumentEntryPO.class);
		List<XdsDocumentEntryPO> stringList = namedParameterJdbcTemplate.query(sql.toString(), map, BeanPropertyRowMapper.newInstance(XdsDocumentEntryPO.class));

		log.info(JSONUtil.toJsonStr(stringList));
		return stringList;
	}
	
	public List<XdsDocumentEntryPO> GET_XdsDocumentEntryPOs(GetDocumentsVO vo )  {
		log.info("入参:"+JSONUtil.toJsonStr(vo));
		StringBuilder sql = new StringBuilder();
		Map<String, Object> map = new HashMap<String, Object>();
		sql.append(" SELECT * FROM camel_xdsdocumententry where 1=1 ");
		if (StringUtils.hasText(vo.get$homeCommunityId())) {
			sql.append("  and homeCommunityId=:homeCommunityId ");
			map.put("homeCommunityId", vo.get$homeCommunityId());
		}
		if (vo.get$XDSDocumentEntryUniqueId()!=null) {
			sql.append("  and uniqueId in (:uniqueId) ");
			map.put("uniqueId", vo.get$XDSDocumentEntryUniqueId());
		}
		if (vo.get$XDSDocumentEntryEntryUUID()!=null) {
			sql.append("  and entryUuid in (:entryUuid) ");
			map.put("entryUuid", vo.get$XDSDocumentEntryEntryUUID());
		}
		log.info(sql.toString());
		List<XdsDocumentEntryPO> stringList = namedParameterJdbcTemplate.query(sql.toString(), map, BeanPropertyRowMapper.newInstance(XdsDocumentEntryPO.class));

		log.info(JSONUtil.toJsonStr(stringList));
		return stringList;
	}
	
	public List<XdsFolderPO> GET_XdsFolderPO(XdsFolderPO vo )  {
		log.info("入参:"+JSONUtil.toJsonStr(vo));
		StringBuilder sql = new StringBuilder();
		Map<String, String> map = new HashMap<String, String>();
		sql.append(" SELECT * FROM camel_xdsfolder where 1=1 ");
		if (StringUtils.hasText(vo.getEntryUuid())) {
			sql.append("  and entryUuid=:entryUuid ");
			map.put("entryUuid", vo.getEntryUuid());
		}
		if (StringUtils.hasText(vo.getPatientId())) {
			sql.append("  and patientId=:patientId ");
			map.put("patientId", vo.getPatientId());
		}
		if (StringUtils.hasText(vo.getAvailabilityStatus())) {
			sql.append("  and availabilityStatus=:availabilityStatus ");
			map.put("availabilityStatus", vo.getAvailabilityStatus());
		}
		log.info(sql.toString());
//		List<XdsFolderPO> stringList = namedParameterJdbcTemplate.queryForList(sql.toString(), map, XdsFolderPO.class);
		List<XdsFolderPO> stringList = namedParameterJdbcTemplate.query(sql.toString(), map, BeanPropertyRowMapper.newInstance(XdsFolderPO.class));

		log.info(JSONUtil.toJsonStr(stringList));
		return stringList;
	}
		

	public List<XdsSubmissionSetPO> GET_XdsSubmissionSetPO(XdsSubmissionSetPO vo )  {
		log.info("入参:"+JSONUtil.toJsonStr(vo));
		StringBuilder sql = new StringBuilder();
		Map<String, String> map = new HashMap<String, String>();
		sql.append(" SELECT * FROM camel_xdssubmissionset where 1=1 ");
		if (StringUtils.hasText(vo.getEntryUuid())) {
			sql.append("  and entryUuid=:entryUuid ");
			map.put("entryUuid", vo.getEntryUuid());
		}
		if (StringUtils.hasText(vo.getHomeCommunityId())) {
			sql.append("  and homeCommunityId=:homeCommunityId ");
			map.put("homeCommunityId", vo.getHomeCommunityId());
		}
		if (StringUtils.hasText(vo.getPatientId())) {
			sql.append("  and patientId=:patientId ");
			map.put("patientId", vo.getPatientId());
		}
		if (StringUtils.hasText(vo.getAvailabilityStatus())) {
			sql.append("  and availabilityStatus=:availabilityStatus ");
			map.put("availabilityStatus", vo.getAvailabilityStatus());
		}
		log.info(sql.toString());
//		List<XdsSubmissionSetPO> stringList = namedParameterJdbcTemplate.queryForList(sql.toString(), map, XdsSubmissionSetPO.class);
		List<XdsSubmissionSetPO> stringList = namedParameterJdbcTemplate.query(sql.toString(), map, BeanPropertyRowMapper.newInstance(XdsSubmissionSetPO.class));
		log.info(JSONUtil.toJsonStr(stringList));
		return stringList;
	}
	
	public List<XdsCache> GET_XdsCache(XdsCache vo )  {
		log.info("入参:"+JSONUtil.toJsonStr(vo));
		StringBuilder sql = new StringBuilder();
		Map<String, String> map = new HashMap<String, String>();
		sql.append(" SELECT * FROM camel_xdscache where 1=1 ");
		if (StringUtils.hasText(vo.getDataEntryUuid())) {
			sql.append("  and dataEntryUuid=:dataEntryUuid ");
			map.put("dataEntryUuid", vo.getDataEntryUuid());
		}
		if (StringUtils.hasText(vo.getDataClass())) {
			sql.append("  and dataClass=:dataClass ");
			map.put("dataClass", vo.getDataClass());
		}
		if (StringUtils.hasText(vo.getDataType())) {
			sql.append("  and dataType=:dataType ");
			map.put("dataType", vo.getDataType());
		}
		log.info(sql.toString());
		List<XdsCache> stringList = namedParameterJdbcTemplate.query(sql.toString(), map, BeanPropertyRowMapper.newInstance(XdsCache.class));
		log.info(JSONUtil.toJsonStr(stringList));
		return stringList;
	}
	
}
